package com.component.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.component.domain.MFilebase64;
import com.component.service.MFilebase64Service;
import com.component.mapper.MFilebase64Mapper;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.Base64;

/**
 *
 */
@Service
public class MFilebase64ServiceImpl extends ServiceImpl<MFilebase64Mapper, MFilebase64>
    implements MFilebase64Service{

    @Override
    public MFilebase64 uploadFile(MultipartFile multipartFile) {
        MFilebase64 mFilebase64=new MFilebase64();
        try {
            mFilebase64.setFilename(multipartFile.getOriginalFilename());
            mFilebase64.setFiletype(multipartFile.getContentType());
            mFilebase64.setFilesize(Double.valueOf(String.valueOf(multipartFile.getSize())));
            String base64String = Base64.getEncoder().encodeToString(multipartFile.getBytes());
            mFilebase64.setFilebase64str(base64String);
            super.save(mFilebase64);
        } catch (IOException e) {
            e.printStackTrace();
        }
        return mFilebase64;
    }

    @Override
    public byte[] fileBase64ToByte(MFilebase64 filebase64) {
        // 将Base64编码字符串转换为字节数组
        byte[] byteArray = Base64.getDecoder().decode(filebase64.getFilebase64str());
        return byteArray;
    }
}




